package com.estronger.shopmanage.ui.aftermarket.adapter;

import android.Manifest;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import com.estronger.shopmanage.R;
import com.estronger.shopmanage.adapter.DefaultAdapter;
import com.estronger.shopmanage.base.BaseActivity;
import com.estronger.shopmanage.base.BaseHolder;
import com.estronger.shopmanage.gaodemap.LocationActivity;
import com.estronger.shopmanage.ui.aftermarket.AftermarketActivity;
import com.estronger.shopmanage.ui.aftermarket.AppointmentDetailActivity;
import com.estronger.shopmanage.ui.aftermarket.AppointmentSureActivity;
import com.estronger.shopmanage.ui.aftermarket.CompletedConfirmActivity;
import com.estronger.shopmanage.ui.aftermarket.bean.AftermarketBean;
import com.estronger.shopmanage.ui.aftermarket.bean.AftermarketItem;
import com.estronger.shopmanage.ui.install.BuildActivity;
import com.estronger.shopmanage.utils.PermissionUtils;
import com.estronger.shopmanage.utils.TimeUtils;
import com.estronger.shopmanage.utils.Toast;
import com.estronger.shopmanage.view.AppDialog;
import com.tbruyelle.rxpermissions2.Permission;

import java.util.List;

public class AftermarketAdapter extends DefaultAdapter<AftermarketItem> {

    public enum ViewType{//哪个类型的视图适配器
        ONE,TWO,ALL
    }
    Context mContext;

    ViewType viewType;

    public void setViewType(ViewType viewType) {
        this.viewType = viewType;
    }

    public AftermarketAdapter(List<AftermarketItem> datas,Context context) {
        super(datas);
        this.mContext = context;
    }

    @Override
    protected BaseHolder<AftermarketItem> getHolder() {
        return new AftermarketHolder();
    }

    class AftermarketHolder extends BaseHolder<AftermarketItem> implements View.OnClickListener{


        boolean needCallBack = false;//记录是否需要跳转返回

        public TextView item_name, item_phone, appointment, appointment_time,
                location;
        public ImageView state, call, sms;
        public LinearLayout item_contact, item_castomer, item_finish, item_bottom;
        private View item_line;
        RelativeLayout relativeLayout;
        Dialog dialog;
        TextView orderText;

        @Override
        public View initView() {
            View view = View.inflate(mContext, R.layout.aftermarket_item_layout, null);
            item_name = (TextView) view.findViewById(R.id.build_item_name);
            item_phone = (TextView) view.findViewById(R.id.build_item_phone);
            location = (TextView) view.findViewById(R.id.build_item_location);
            appointment = (TextView) view.findViewById(R.id.appointment);
            appointment_time = (TextView) view
                    .findViewById(R.id.appointment_time);
            state = (ImageView) view.findViewById(R.id.build_item_state);
            item_contact = (LinearLayout) view
                    .findViewById(R.id.build_item_contact);
            item_castomer = (LinearLayout) view
                    .findViewById(R.id.build_item_castomer);
            item_finish = (LinearLayout) view
                    .findViewById(R.id.build_item_finish);
            item_bottom = (LinearLayout) view
                    .findViewById(R.id.build_item_bottom);
            item_line = view.findViewById(R.id.build_item_line);
            call = (ImageView) view.findViewById(R.id.build_phone_call);
            sms = (ImageView) view.findViewById(R.id.build_send_sms);
            orderText = view.findViewById(R.id.oderNum);
            call.setOnClickListener(this);
            sms.setOnClickListener(this);
            item_phone.setOnClickListener(this);
            location.setOnClickListener(this);
            view.setOnClickListener(this);
            relativeLayout = view.findViewById(R.id.build_item_layout);
            item_contact.setOnClickListener(this);
            item_finish.setOnClickListener(this);
            switch (viewType){
                case ONE:
                    state.setImageResource(R.drawable.state_appointment_icon);
                    state.setTag("0");
                    item_contact.setVisibility(View.VISIBLE);
                    item_finish.setVisibility(View.GONE);
                    break;
                case TWO:
                    state.setImageResource(R.drawable.state_install_icon);
                    item_finish.setVisibility(View.VISIBLE);
                    item_contact.setVisibility(View.GONE);
                    break;
                case ALL:
                    state.setImageResource(R.drawable.state_appointment_icon);
                    state.setTag("0");
                    item_contact.setVisibility(View.GONE);
                    item_finish.setVisibility(View.GONE);
                    item_bottom.setVisibility(View.GONE);
                    break;

                    default:break;
            }
            return view;
        }

        @Override
        public void refreshView(AftermarketItem aftermarketItem) {
            item_name.setText(aftermarketItem.getFcstname());
            item_phone.setText(aftermarketItem.getFcstmobile());
            location.setText(aftermarketItem.getFaddress());
            appointment.setText("预约时间：" + aftermarketItem.getFplantime());
            appointment_time.setVisibility(aftermarketItem.getFstate().equals("1")?View.GONE:View.VISIBLE);
            appointment_time.setText(TimeUtils.chaZhi(aftermarketItem.getFplantime()));
            orderText.setText(aftermarketItem.getFclaimwono());
        }

        @Override
        public void onClick(final View v) {
            final BaseActivity activity = (BaseActivity)mContext;
            Intent intent;
            switch (v.getId()){
                case R.id.build_item_layout://外部整个item、跳转详情
                    intent = new Intent(mContext, AppointmentDetailActivity.class);
                    intent.putExtra("dlvid",data.getFtaskid());
                    mContext.startActivity(intent);
                    break;
                case R.id.build_phone_call:// 点击了送货条目上的电话小图标
                case R.id.build_item_contact:// 点击了送货条目上的与客户确认预约
                    String number = item_phone.getText().toString();
                    if (activity.isRightNumber(number)) {
                        dialog = AppDialog.createDialog(activity,
                                data.getFcstname(), number, this, data.getFtaskid());
                        if (v.getId() == R.id.build_item_contact) {
                            needCallBack = true;
                        } else {
                            needCallBack = false;
                        }
                        dialog.show();
                    }
                    break;
                case R.id.build_send_sms:
                    String number1 = item_phone.getText().toString();
                    if (activity.isRightNumber(number1)) {
                        Uri uri = Uri.parse("smsto:" + number1);
                        intent = new Intent(Intent.ACTION_SENDTO, uri);
                        mContext.startActivity(intent);
                    }
                    break;
                case R.id.ok:
                    String number2 = item_phone.getText().toString();
                    AftermarketActivity.send_deliv_id = data.getFtaskid();
                    intent = new Intent(Intent.ACTION_DIAL, Uri.parse("tel:"
                            + number2));
                    if (needCallBack) {
                        activity.startActivityForResult(intent,
                                100);
                    } else {
                        mContext.startActivity(intent);
                    }
                    dialog.dismiss();
                    break;
                case R.id.build_item_location:
                    PermissionUtils.getInstance().requestPromisssions(activity, new PermissionUtils.AcceptListener() {
                                @Override
                                public void grante() {
                                    TextView tv = (TextView) v;
                                    String add = tv.getText().toString().trim();
                                    if (!add.equals("")) {
                                        Intent intent = new Intent(mContext, LocationActivity.class);
                                        intent.putExtra("latitude", "");
                                        intent.putExtra("location", add);
                                        intent.putExtra("longtitude", "");
                                        activity.startActivityForResult(intent, 8889);
                                    } else {
                                        Toast.show(mContext, "安装客户地址为空", 2000);
                                    }
                                }

                                @Override
                                public void denial(Permission permission) {

                                }

                                @Override
                                public void fail() {

                                }
                            },Manifest.permission.ACCESS_COARSE_LOCATION,
                            Manifest.permission.ACCESS_FINE_LOCATION);
                    break;
                    case R.id.build_item_finish:
                        intent = new Intent(mContext, CompletedConfirmActivity.class);
                        intent.putExtra("taskId", data.getFtaskid());
                        activity.startActivityForResult(intent,
                                BuildActivity.REFLESH_DATA);
                        break;
            }
        }
    }
}
